/*
 * Copyright (c) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.generator.discovery;

import com.google.api.client.generator.discovery.model.ClassModel;
import com.google.api.client.generator.discovery.model.PropertyModel;
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;

import java.io.PrintWriter;

/**
 * @author Yaniv Inbar
 */
public class ModelFileGenerator extends AbstractDiscoveryJavaFileGenerator {

  private final ClassModel classModel;

  protected ModelFileGenerator(ClassModel classModel) {
    super(classModel.getParentService(), classModel.getPackageName(), classModel.getName());
    this.classModel = classModel;
  }

  @Override
  protected void generateBody(PrintWriter out) {
    // javadoc
    writeJavaDoc(out, 2, classModel);
    // public final class MyClass extends GenericJson {
    out.println(
        "public final class " + getClassName() + " extends " + useClass(GenericJson.class) + " {");
    // properties
    for (PropertyModel property : classModel.getProperties()) {
      // javadoc
      out.println();
      writeJavaDoc(out, 2, property);
      // @Key("property")
      out.print(indent(2) + "@" + useClass(Key.class));
      if (!property.getName().equals(property.getWireName())) {
        out.print("(\"" + property.getWireName() + "\")");
      }
      out.println();
      // private String property;
      out.println(indent(2) + "private " + useType(property) + " " + property.getName() + ";");
      // javadoc
      out.println();
      writeJavaDoc(out, 2, property);
      // public String getProperty() {
      out.println(indent(2) + "public " + useType(property) + " " + property.getGetterMethodName()
          + "() {");
      // return property;
      out.println(indent(4) + "return " + property.getName() + ";");
      // }
      out.println(indent(2) + "}");
      // javadoc
      out.println();
      writeJavaDoc(out, 2, property);
      // public MyClass setProperty(String property) {
      out.println(
          indent(2) + "public " + getClassName() + " " + property.getSetterMethodName() + "("
              + useType(property) + " " + property.getName() + ") {");
      // this.property = property;
      out.println(indent(4) + "this." + property.getName() + " = " + property.getName() + ";");
      // }
      out.println(indent(2) + "}");
    }
    // }
    out.println("}");
    out.close();
  }
}
